package tool;

import java.util.regex.Pattern;

import lib.Global;
import lib.KeyMa;

import org.apache.log4j.Logger;

import reference.TokenMa;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.examples.oauth2.Log;
import weibo4j.model.User;
import weibo4j.model.WeiboException;

public class MUser {
	public static void GetUidByScreenName(String name) throws InterruptedException{
		Weibo weibo = new Weibo();
		TokenMa aToken = new TokenMa();
		weibo.setToken(aToken.getToken());
		
		Users um = new Users();
		try {
			User user = um.showUserByScreenName("可爱麦兜族");
			Log.logInfo(user.toString());
			//add to do
		} catch (WeiboException e) {
			e.printStackTrace();
		}finally{
			Thread.sleep(Global.WAITTIME);
			}
	}
	
	public static User GetUserByUid(String uid){
		Logger logger = Global.getLogger(MUser.class.getName());
		
		Weibo weibo = new Weibo();
		KeyMa aToken = new KeyMa();
		
		Users um = new Users();
		User user;
		int attempt = 0;
		
		while(attempt != 3){
			try {
				weibo.setToken(aToken.getToken());
				user = um.showUserById(uid);
				if(user == null){
					logger.warn(uid + " get null basic info, try again");
					++attempt;
				}else{
					return user;
				}
			} catch (WeiboException e) {
				Pattern p = Pattern.compile(".*error_code:20003.*");
				if(p.matcher(e.getMessage()).matches()){
					logger.warn(uid+" does not exist.");
					attempt = 3;
				}else
				{
					logger.warn(uid + e.getMessage()+". try again");
					++attempt;
				}
				
			}
		}
		
		logger.error(uid+" get basic info failed, skip.");
		return null;
		
	}
}
